import { CreateModalList } from "@/biz-components/hoc-form/with-list";
import { xhrPostJson } from "@/services";
import {onFieldValueChange, onFormMount} from "@formily/core";
import { message } from "antd";
import { useRef, useState } from "react";
import { history } from "umi";
import api, { numberCreate, numberUpdate, numberDelete } from './services';

const WithListComp = CreateModalList({});
const codePattern = /^[a-zA-Z0-9]*$/;
const FilingCabinet = (props: any) => {
    const formRef = useRef();
    const { match } = props;
    const [refresh, setRefresh] = useState(false);
    /**
     * 创建、编辑
     * @param values
     */
    const handleSave = async (values: any) => {
      return values.viewType === 'EDIT' ? await numberUpdate({
        ...values,
        pageCode: 'ArcvNumber_CUD'
      }) : await numberCreate({
        ...values,
        pageCode: 'ArcvNumber_CUD'
      });
    };

    return (
      <>
        <WithListComp
          match={match}
          history={history}
          reloadFlag={refresh}
          pageCode={"ArcvNumber_L"}
          pageApi={api.page}
          modalCode={"ArcvNumber_CUD"}
          noModal={false}
          noCache
          handleSave={handleSave}
          formConfigs={{
            effects: (form: any) => {
              formRef.current = form;
            },
          }}
          modalformConfigs={{
            effects: () => {
              onFieldValueChange('coCode', (field, form) => {
                if (!field.modified) return;
                form.setValuesIn('coName', field.inputValues?.[1]?.name);
              })

                onFormMount((f) => {
                    f.query('code').take((field) => {
                        field.setValidator({
                            validator: () => {
                                if(!codePattern.test(field.value)) {
                                    return "编码只能为数字或字母";
                                }
                                return "";
                            },
                        });
                    })

                    f.query('coCode').take((field) => {
                        field.setValidator({
                            validator: () => {
                                if(!codePattern.test(field.value)) {
                                    return "编码只能为数字或字母";
                                }
                                return "";
                            },
                        });
                    })
                })
            }
          }}
          events={{
            buttonsEvents: {

            },
            tableButtonsEvents: {
              handleDeleteClick: (key: string, row: any) => {
                numberDelete({
                  id: row.id,
                  pageCode: 'ArcvNumber_CUD'
                }).then((res: any) => {
                  if (res.success) {
                    message.success('删除成功');
                    setRefresh(!refresh);
                  }
                })
              }
            },
          }}
        />
      </>
    );
  };

  export default FilingCabinet;
